package app.controllers.dishes;

import app.models.dishes.Tag;
import com.ifonly.activerecord.DbUtils;
import com.ifonly.activerecord.sql.config.Sql;
import com.ifonly.mvc.ActionKey;
import com.ifonly.mvc.Controller;

/**
 * @author ifonly
 * @version 1.0 2016-02-02 08:44
 * @since JDK 1.6
 */
public class TagController extends Controller {

    @ActionKey("")
    public void index() {
        render("index");
    }

    public void dt() {
        renderDataTables(Tag.class);
    }

    public void delete() {
        int id = getInt(0, 0);
        if (id == 0) {
            renderFailed("参数错误,操作失败");
        } else {
            DbUtils.delete(Sql.get("tag.deleteById"), id);
            renderSuccess();
        }
    }

    public void item() {
        int id = getInt(0, 0);
        Tag tag;
        if (id == 0) {
            tag = new Tag();
        } else {
            tag = Tag.findByPrimaryKey(Tag.class, id);
        }
        setAttr("tag", tag);
        render("item");
    }

    public void save() {
        Tag tag = getModel(Tag.class);
        if (DbUtils.isNew(tag)) {
            tag.save();
        } else {
            tag.update();
        }
        redirect("");
    }

}
